<?php

class TemplateLoop extends TemplateParser
{

  protected function render()
  {
    $output = '';

    extract($this->params);

    $data = $this->parseVar($data);

    $output = "
      <?php
      \${$id}['count'] = count($data);
      \${$id}['index'] = 0;
      foreach ($data as \${$id}['item']) {
        \${$id}['first'] = (\${$id}['index'] == 0);
        \${$id}['last'] = (\${$id}['index'] == \${$id}['count'] - 1);
        \${$id}['odd'] = (\${$id}['index'] % 2 == 0);
        \${$id}['even'] = ! \${$id}['odd'];
        \${$id}['number'] = \${$id}['index'] + 1;
        ?>"
      . $this->content
      . "<?php
        \${$id}['index']++;
      } ?>
    ";

    return $this->parseVars($output);
  }

}
